package site.xuqing.autohelper.util;

import android.view.accessibility.AccessibilityNodeInfo;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListTraverseUtil {
    private Map<String, Boolean> traverseMark = new HashMap<>();
    public Map<String,Boolean> getTraverseMark(){
        return this.traverseMark;
    }
    public boolean isAllTraverse(List<AccessibilityNodeInfo> nodes) {
        for (AccessibilityNodeInfo node : nodes) {
            String sourceIdentity = getSourceIdentity(node);
            if (!traverseMark.containsKey(sourceIdentity) || !traverseMark.get(sourceIdentity)) {
                return false;
            }
        }
        return true;
    }

    public String getSourceIdentity(AccessibilityNodeInfo node) {
        return getSourceNodeId(node) + node.getText().toString().trim();
    }

    private long getSourceNodeId(AccessibilityNodeInfo node) {
        if (node == null) {
            return 0;
        }
        try {
            Method method = node.getClass().getDeclaredMethod("getSourceNodeId");
            method.setAccessible(true);
            return (long) method.invoke(node, new Object[]{});
        } catch (Exception e) {
            System.out.println("找不到getSourceNodeId方法：" + e);
            return 0;
        }
    }
}
